/*
 * 图文与文字的切换，加载素材列表
 */
var $MoveTools = {
	config:{
		'listSelector': '#news-lists',
		'targetListSelector': '.news-edit-lists',
		'valueInputSelector': '#news-id',
		'data': news_data,
		'idPrefix' : 'id-',
		'src_data': {},
		'limit':	8,
		'radio': true,
		'listUrl': "http://5.wmei.sinaapp.com/index.php/Partner/AdPosition/getAdListPost/p/",
	},
	listeners:{
		'listComplete' : function(data) {
			var newLists = "";
			if(data.error == 0){
				if(data.extra){
					for(var i=0, len = data.extra.length; i<len; i++){
						
						$MoveTools.config.src_data[data.extra[i]['id']] = data.extra[i];
						$MoveTools.addItemToList(data.extra[i]);
					}
					
					$($MoveTools.config['listSelector']).append( "<span id='get-more-news' class='get-more-news'>加载更多</span>" );
					
				}else{
					
					$($MoveTools.config['listSelector']).append( "<span id='get-more-news' class='get-more-news'>没有更多了</span>" );
					
				}
				
			}else{
				console.log(data);
			}
			
			//加载更多
			$("#get-more-news").click(function(){
				$(this).remove();
				$MoveTools.initList();
			});
		}
	},
	init:function(cfg){
		if($MoveTools.ready) return;
		else
			$MoveTools.ready = 1;
		if(cfg)
			for(var o in cfg){
				$MoveTools.config[o] = cfg[o];
			}
		this.pNum = 1;
		// 初始化列表
		this.initList();
		// 刷新目标列表
		this.refresh();
		//事件绑定
		this.bindListEvent();
		this.bindTargetListEvent();
	},
	// 绑定列表事件
	bindListEvent:function(){
		//点击选择图文
		$($MoveTools.config['listSelector']).delegate("li", "click", function(){
			
			var dataLength =  $MoveTools.getJsonLength( $MoveTools.config.data); //获取已选择的图文长度
			
			if( $(this).hasClass("selected") || ( dataLength >　0 ) ){
				return false;
			} else {
				//选择图文状态，添加至左侧，并获取图文id
				$(this).addClass("selected");
				$MoveTools.config.data[$MoveTools.config['idPrefix']+$(this).attr("data-id")] = $MoveTools.config.src_data[$(this).attr("data-id")];
				$MoveTools.refresh();
			}
		});
	},
	// 绑定目标列表事件
	bindTargetListEvent:function(){
		
		$($MoveTools.config['targetListSelector']).delegate("li", "mouseenter", function(){
			$(this).children(".news-mask").show();
			$(this).children(".icon-news-selected").show();
		});
		
		$($MoveTools.config['targetListSelector']).delegate("li", "mouseleave", function(){
			$(this).children(".news-mask").hide();
			$(this).children(".icon-news-selected").hide();
		});
		
		$($MoveTools.config['targetListSelector']).delegate("li","click",function(){
			var id = $(this).attr('data-id');
			delete $MoveTools.config.data[id];
			$( $MoveTools.config['listSelector']+" li[data-id="+id.replace($MoveTools.config['idPrefix'],"")+"]" ).removeClass("selected");
			$MoveTools.refresh();
		})
	},
	// 初始化列表
	initList:function(){
		$.post(this.config['listUrl'] + this.pNum, {}, this.listeners['listComplete']);
		this.pNum++;
	},
	// 添加原始数据列表
	addItemToList:function(item){
		
		//已经选择过的图文处于选择状态
		var c = "",
			idPre = $MoveTools.config.idPrefix;
		if($MoveTools.config.data[idPre + item['id']]){ c = "selected"; }
		var html = "<li class='news-item " + c + "' data-id="+ item['id'] +"><div class='news-content'><div class='news-title'><h4>"+item['name']+"</h4></div><div class='news-image'><img src='"+item['pic']+"'/></div></div><div class='news-mask'></div><div class='icon-news-selected'></div></li>";
		$($MoveTools.config['listSelector']).append( html );
		
	},
	// 添加目标数据列表
	addItemToTargetList:function(item){
		var html = "<div class='news-content'><div class='news-title'><h4>"+item['name']+"</h4></div><div class='news-image'><img src='"+item['pic']+"'/></div></div><div class='news-mask'></div><div class='icon-news-selected'></div>";
		html = "<li class='news-edit-item news-edit-item2' data-id='"+ ($MoveTools.config['idPrefix']+item['id']) + "' >" + html + "</li>";
		$($MoveTools.config['targetListSelector']).append( html );
	},
	// 刷新已选择数据列表
	refresh:function(){
		$($MoveTools.config['targetListSelector']).html('');
		for(var i in $MoveTools.config.data){
			$MoveTools.addItemToTargetList($MoveTools.config.data[i]);
		}
		$($MoveTools.config['targetListSelector']+" li").eq(0).removeClass("news-edit-item2").addClass("news-edit-item1");
		$MoveTools.getVal();
	},
	getVal:function(){
		(function(json){
			var arr = new Array();
			for(var i in json){
				arr.push(json[i]['id']);
			}
			$($MoveTools.config['valueInputSelector']).val( arr.join(",") );
		})($MoveTools.config.data);
	},
	getJsonLength: function(json){
		var length = 0;
		for(var i in json){
			length++;
		}
		return length;
	}
};